<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');
/**
* This controller is used for JAEmail feature of the component
*/
class JAJobBoardControllerJACustomtmpl extends JAJobBoardController {
/**
     * Constructor
     */
    function __construct(  )
    {
    	
        parent::__construct(  ); 
		checkPermission_Administrator('customize_template');             
        // Register Extra tasks
        $this->registerTask( 'apply',   'save' );        
    }
    
    /**
    * Display current jacustomtmpl of the component to administrator
    * 
    */
    function display(){
    	
    	switch($this->getTask())
		{			
			case 'edit':
			{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'edit', true );
				JRequest::setVar( 'layout', 'form' );				
			} break;
		}
		
		parent::display();
    }
    
    /**
    * Cancel current operation
    * 
    */
    function cancel(){
    	global $option;
        $this->setRedirect("index.php?option=$option&view=jacustomtmpl");
    }       
	
	/**
	* Save categories record
	*/
	function save(){
		global $option;
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$content = JRequest::getVar('content', '', 'post', 'string', JREQUEST_ALLOWRAW );
		
		$file = JRequest::getCmd('file', '');
		$folder = JRequest::getCmd('folder', '');
		
		$template = getTemplate();
		
		$path = JPATH_SITE . DS. 'templates'. DS . $template . DS. 'html'. DS. 'com_jajobboard'. DS . $folder. DS  .$file;	
		
		$res = JFile::write($path, $content);
		
		if ($res) {
			$msg = JText::_( 'Successfully updated file').': '.$file;
		} else {
			$msg = JText::_( 'Cannot saving file '.$file );
		}
		
		switch ( $this->_task ) {
			case 'apply':
				$this->setRedirect( "index.php?option=$option&view=jacustomtmpl&task=edit&file=$file&folder=$folder" , $msg );
				break;

			case 'save':
			default:
				$this->setRedirect( "index.php?option=$option&view=jacustomtmpl", $msg );
				break;
		}
	}		
	
	function restore(){
		global $option;
		
		$file = JRequest::getVar('file', '');
		$folder = JRequest::getVar('folder', '');
		
		$template = getTemplate();
		
		$path = JPATH_SITE . DS. 'templates'. DS . $template . DS. 'html'. DS. 'com_jajobboard'. DS . $folder. DS  .$file;
		$res = false;	
		if(JFile::exists($path)){
			$res = JFile::delete($path);
		}		
		
		$msg = '';
		if ($res) {
			$msg = JText::_( 'Restore file Successfully').': '.$file;
		} else {
			$msg = JText::_( 'Cannot restore file '.$file );
		}
		
		$this->setRedirect( "index.php?option=$option&view=jacustomtmpl", $msg );
	}
		
}